package com.shopping.controller;

import com.shopping.bean.Comment;
import com.shopping.bean.User;
import com.shopping.service.impl.CommentService;
import com.shopping.util.LayData;
import com.shopping.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    @RequestMapping("listByProductId")
    @ResponseBody
    public Map<String, Object> getCommentsByProductId(@RequestParam("productid") int productid,
                                                      @RequestParam(value = "page", defaultValue = "1") int page,
                                                      @RequestParam(value = "limit", defaultValue = "10") int limit,
                                                      @RequestParam(value = "type", required = false) Integer type) {
        return commentService.listByProductId(productid, page, limit, type);
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> addComment(@RequestBody Comment comment, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        // 从会话中获取登录用户信息
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (loginUser == null) {
            // 用户未登录，返回错误信息
            result.put("code", 401);
            result.put("msg", "请先登录");
            return result;
        }
        // 设置用户 ID
        int userId = loginUser.getUserid();
        comment.setUserid(userId);

        // 检查商品 ID 是否存在
        if (comment.getProductid() <= 0) {
            result.put("code", 400);
            result.put("msg", "商品 ID 无效");
            return result;
        }

        try {
            // 设置评论添加时间
            comment.setAddtime(new Date());
            // 调用服务层方法添加评论
            boolean success = commentService.addComment(comment);
            if (success) {
                result.put("code", 200);
                result.put("msg", "评论提交成功");
            } else {
                result.put("code", 500);
                result.put("msg", "评论提交失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "评论提交失败: " + e.getMessage());
        }
        return result;
    }

    @RequestMapping("listByUserId")
    @ResponseBody
    public Map<String, Object> getCommentsByUserId(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (loginUser == null) {
            result.put("code", 401);
            result.put("msg", "请先登录");
            return result;
        }
        int userId = loginUser.getUserid();
        try {
            List<Comment> comments = commentService.listByUserId(userId);
            result.put("code", 200);
            result.put("data", comments);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取评论列表失败: " + e.getMessage());
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public LayData deleteComment(@RequestParam("id") Integer commentid) {
        int result = commentService.deleteComment(commentid);
        if (result > 0) {
            return LayData.success(200, "删除成功");
        } else {
            return LayData.success(400, "删除失败");
        }
    }
}